package org.apache.cxf.interceptor;

/**
 * <p><em>Created on 2022/4/8 2:05 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class PostInvokeInterceptor extends AbstractPhaseInterceptor<Message> {
    private final static Logger logger = LoggerFactory.getLogger(PostInvokeInterceptor.class);

    public PostInvokeInterceptor() {
        super(Phase.POST_INVOKE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        InputStream  inputStream = message.getContent(InputStream.class);
        CachedOutputStream bosLog = new CachedOutputStream();
        CachedOutputStream bosNew = null;
        try {
            try {

                //将返回信息转换成json格式
                String res = "";

                //调用工具类返回自定义消息
                resMessage(message, res);

                //终止拦截链，不会往下执行到接口方法
                message.getInterceptorChain().abort();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally {
            try {
                bosLog.close();
                if (null != bosNew){
                    bosNew.close();
                }
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void resMessage(Message arg0, String message) {

        logger.info("返回请求拦截信息：" + message);

        try {

            HttpServletResponse response = (HttpServletResponse) arg0
                    .get(AbstractHTTPDestination.HTTP_RESPONSE);

            ServletOutputStream out = response.getOutputStream();

            try {
                out.write(message.getBytes("utf-8"));
                out.flush();
                arg0.getInterceptorChain().doInterceptStartingAfter(arg0,
                        "org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor");
            } catch (Exception e) {
                e.printStackTrace();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
